const { exec, spawn } = require("child_process");

const createExecution = (command) => {
  return () => {
    return new Promise((resolve, reject) => {
      exec(command, (error, stdout, stderr) => {
        if (error) {
          reject(error);
        } else {
          resolve(stdout);
          // 注意：确保处理完毕后终止子进程

        }
      });
    });
  };
};

const execCommands = async (commands) => {
  const executions = commands.map((command) => createExecution(command));
  for (const item of executions) {
    await item();
  }
};

const spawnExec = function(...args){
  return new Promise(resolve=>{
      const proc = spawn(...args)
      proc.stdout.pipe(process.stdout)
      proc.stderr.pipe(process.stderr)
      proc.on('close', () => {
          resolve();
      })
  })
}

module.exports.exec = execCommands;

module.exports.spawnExec = spawnExec;